<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=0.45, user-scalable=yes">
    <title>全部猫咪详情</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        table {
            caption-side: top;
        }
    </style>
</head>
<body>
<h2 id="headText" style="margin-left: 0.5%;margin-top: 0.3%"></h2>
<hr>
<div id="app">
    <input type="button" class="btn btn-primary btn-sm"
           onclick="location.href = '/CampusCats/home'" value="回主页" style="margin-left: 0.5%">
    <input type="button" class="btn btn-primary btn-sm"
           onclick="location.href = '/CampusCats/addCat'" value="添加新猫咪">
    <input type="button" class="btn btn-primary btn-sm"
           onclick="location.href = '/CampusCats/userRecords'" value="查看我的记录">
    <hr>
    <table class="table table-striped" id="catTbl">
        <caption style="margin-left: 0.5%">所有猫咪信息</caption>
        <tr>
            <th>编号</th>
            <th>名字</th>
            <th>品种</th>
            <th>颜色</th>
            <th>性格</th>
            <th>常出现位置</th>
            <th>记录数</th>
            <th>操作</th>
        </tr>
        <tr v-for="cat in cats.data">
            <td>{{cat.catId}}</td>
            <td>{{cat.catName}}</td>
            <td>{{cat.category}}</td>
            <td>{{cat.color}}</td>
            <td>{{cat.character}}</td>
            <td>{{cat.location}}</td>
            <td>{{cat.recordCount}}</td>
            <td>
                <div id="links" class="btn-group-sm">
                    <input type="button" @click="add('appearance', cat.catId)" class="btn btn-primary" value="添加出现记录">
                    <input type="button" @click="add('feeding', cat.catId)" class="btn btn-primary" value="添加投喂记录">
                    <input type="button" @click="catRecords(cat.catId)" class="btn btn-primary" value="查看所有记录">
                    <span id="adminOp" v-if="user.isAdmin" class="btn-group-sm">
                        <input type="button" @click="upd(cat.catId)" class="btn btn-primary" value="修改">
                        <input type="button" @click="del(cat.catId)" class="btn btn-primary" value="删除">
                    </span>
                </div>
            </td>
        </tr>
    </table>
</div>

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script>
<script src="/CampusCats/js/userMsg.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            user: {},
            cats: {}
        },
        mounted: async function() {
            this.user = await userMsg("");
            await this.flush();
        },
        methods: {
            async flush() {
                this.cats = (await myAxios.get('/cats')).data;
                if (this.cats.code !== 20021) {
                    alert("错误：" + this.cats.msg + "\n错误代码：" + this.cats.code);
                }
            },
            add(recordType, id) {
                location.href = '/CampusCats/addRecord?type=' + recordType + '&catId=' + id;
            },
            catRecords(id) {
                location.href = '/CampusCats/catRecords?catId=' + id;
            },
            upd(id) {
                location.href = '/CampusCats/adminUpdateCat?catId=' + id;
            },
            async del(id) {
                let del = (await myAxios.delete('/admin/cats/' + id)).data;
                if (del.code !== 20041) {
                    alert("错误：" + del.msg + "\n错误代码：" + del.code);
                }
                await this.flush();
            }
        }
    });
</script>
</body>
</html>